package com.castle.fortress.admin.oa.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.castle.fortress.admin.oa.enums.AttendanceRecordTypeEnum;
import com.castle.fortress.admin.system.entity.SysUser;
import com.castle.fortress.admin.utils.WebUtil;
import com.castle.fortress.common.utils.ConvertUtil;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.castle.fortress.admin.oa.entity.AttendanceRecordEntity;
import com.castle.fortress.admin.oa.dto.AttendanceRecordDto;
import com.castle.fortress.admin.oa.mapper.AttendanceRecordMapper;
import com.castle.fortress.admin.oa.service.AttendanceRecordService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.HashMap;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.util.List;
import com.castle.fortress.admin.utils.BizCommonUtil;

/**
 * 上下班考勤记录 服务实现类
 *
 * @author whc
 * @since 2022-11-04
 */
@Service
public class AttendanceRecordServiceImpl extends ServiceImpl<AttendanceRecordMapper, AttendanceRecordEntity> implements AttendanceRecordService {
    /**
    * 获取查询条件
    * @param attendanceRecordDto
    * @return
    */
    public QueryWrapper<AttendanceRecordEntity> getWrapper(AttendanceRecordDto attendanceRecordDto){
        QueryWrapper<AttendanceRecordEntity> wrapper= new QueryWrapper();
        if(attendanceRecordDto != null){
            AttendanceRecordEntity attendanceRecordEntity = ConvertUtil.transformObj(attendanceRecordDto,AttendanceRecordEntity.class);
            wrapper.eq(attendanceRecordEntity.getUserId() != null,"user_id",attendanceRecordEntity.getUserId());
            wrapper.eq(attendanceRecordEntity.getType() != null,"type",attendanceRecordEntity.getType());
            wrapper.eq(attendanceRecordEntity.getStatus() != null,"status",attendanceRecordEntity.getStatus());
            wrapper.orderByDesc("create_time");
        }
        return wrapper;
    }


    @Override
    public IPage<AttendanceRecordDto> pageAttendanceRecord(Page<AttendanceRecordDto> page, AttendanceRecordDto attendanceRecordDto) {
        QueryWrapper<AttendanceRecordEntity> wrapper = getWrapper(attendanceRecordDto);
        Page<AttendanceRecordEntity> pageEntity = new Page(page.getCurrent(), page.getSize());
        Page<AttendanceRecordEntity> attendanceRecordPage=baseMapper.selectPage(pageEntity,wrapper);
        Page<AttendanceRecordDto> pageDto = new Page(attendanceRecordPage.getCurrent(), attendanceRecordPage.getSize(),attendanceRecordPage.getTotal());
        pageDto.setRecords(ConvertUtil.transformObjList(attendanceRecordPage.getRecords(),AttendanceRecordDto.class));
        return pageDto;
    }

	@Override
	public IPage<AttendanceRecordDto> pageAttendanceRecordExtends(Page<AttendanceRecordDto> page, AttendanceRecordDto attendanceRecordDto){
		Map<String,Long> pageMap = BizCommonUtil.getPageParam(page);
          AttendanceRecordEntity attendanceRecordEntity = ConvertUtil.transformObj(attendanceRecordDto,AttendanceRecordEntity.class);
		List<AttendanceRecordEntity> attendanceRecordList=baseMapper.extendsList(pageMap,attendanceRecordEntity);
		Long total = baseMapper.extendsCount(attendanceRecordEntity);
		Page<AttendanceRecordDto> pageDto = new Page(page.getCurrent(), page.getSize(),total);
		pageDto.setRecords(ConvertUtil.transformObjList(attendanceRecordList,AttendanceRecordDto.class));
		return pageDto;
	}
	@Override
	public AttendanceRecordDto getByIdExtends(Long id){
          AttendanceRecordEntity  attendanceRecordEntity = baseMapper.getByIdExtends(id);
		return ConvertUtil.transformObj(attendanceRecordEntity,AttendanceRecordDto.class);
	}

    @Override
    public List<AttendanceRecordDto> listAttendanceRecord(AttendanceRecordDto attendanceRecordDto){
        QueryWrapper<AttendanceRecordEntity> wrapper = getWrapper(attendanceRecordDto);
        List<AttendanceRecordEntity> list = baseMapper.selectList(wrapper);
        return ConvertUtil.transformObjList(list,AttendanceRecordDto.class);
    }

    @Override
    public AttendanceRecordEntity getByDate(HashMap<String, Object> query) {
        SysUser currentUser = WebUtil.currentUser();
        String today = DateUtil.today();
        QueryWrapper<AttendanceRecordEntity> pmWrapper = new QueryWrapper<>();
        pmWrapper.eq("user_id", query.get("user_id") == null ? currentUser.getLoginName() : query.get("user_id"));
        pmWrapper.eq("date", query.get("date") == null ? today : query.get("date"));
        pmWrapper.eq("type", query.get("type") == null ? AttendanceRecordTypeEnum.AM.getCode() : query.get("type"));
        return baseMapper.selectOne(pmWrapper);
    }
}

